// 课件编辑部分的相关混入
import coursewareApi from "@rc/apis/rc/courseware.mjs";

import EditMixinEditDialog from "../components/courseware-edit/layout-dialog.vue";
import EditMixinEditBase from "../components/courseware-edit/base.vue";
import EditMixinEditProject from "../components/courseware-edit/project.vue";
import EditMixinEditOtherMessage from "../components/courseware-edit/other-message.vue";

export default {
	components: {
		EditMixinEditDialog,
		EditMixinEditBase,
		EditMixinEditProject,
		EditMixinEditOtherMessage
	},
	data() {
		return {
			editMixin_baseLoading: false, // 基础信息编辑的加载状态
			editMixin_projectLoading: false, // 项目信息编辑的加载状态
			editMixin_isShowEditDialog: false, // 是否显示编辑弹窗
			editMixin_otherLoading: false, // 编辑其他信息的加载状态
			editMixin_AuditLoading: false, // 审核通过的加载状态
			// 当前编辑的课件详情
			editMixin_editInfo: {
				resourceId: "", //视频资源主键
				name: "", // 视频资源名称
				sectionNumber: "", // 节数
				sourceFileName: "", // 源文件名称
				videoVid: "", // 视频vid
				teacherId: "", // 授课老师标识
				professorName: "", // 授课老师名称
                livePlaybackState: "N", // 直播回放状态
				rcDisposeResourceVideoProjectResponseVos: [], // 项目列表
				rcResourceLabelInfoResponseVoList: [], // 标签列表
				rcResourceAreaResponseVos: [] // 地区列表
			}
		};
	},
	computed: {
		// 当前编辑的课件基础详情
		editMixin_editBaseInfo() {
			return {
				sectionNumber: this.editMixin_editInfo.sectionNumber,
				name: this.editMixin_editInfo.name,
				teacherId: this.editMixin_editInfo.teacherId,
				professorName: this.editMixin_editInfo.professorName,
				sourceFileName: this.editMixin_editInfo.sourceFileName,
                videoVid: this.editMixin_editInfo.videoVid,
                livePlaybackState: this.editMixin_editInfo.livePlaybackState,
			};
		},
		// 当前编辑的课件项目详情
		editMixin_editProjectInfo() {
			// 单个编辑状态，传入详情
			return this.editMixin_editInfo.rcDisposeResourceVideoProjectResponseVos.map((i) => ({
				...i,
				projectId: [i.projectId],
				subjectList: i.subjectList.map((i2) => i2.subjectId),
                subjectFullList: i.subjectList,
				seriesCourseList: i.seriesCourseList.map((i2) => i2.seriesCourseId)
			}));
		},
		// 当前编辑的课件项目详情
		editMixin_editOtherInfo() {
			// 单个编辑状态，传入详情
			const { resourceId, rcResourceLabelInfoResponseVoList, rcResourceAreaResponseVos } = this.editMixin_editInfo;
			return {
				resourceId, //视频资源主键
				rcResourceLabelInfoResponseVoList, // 标签列表
				rcResourceAreaResponseVos // 地区列表
			};
		}
	},
	methods: {
		// 点击单个编辑
		editMixin_onEdit(resourceId) {
			coursewareApi.queryResourceInfo({
				data: { resourceId },
				success: (data) => {
					this.editMixin_editInfo = data;
					this.editMixin_baseLoading = false; // 基础信息编辑的加载状态
					this.editMixin_projectLoading = false; // 项目信息编辑的加载状态
					this.editMixin_isShowEditDialog = false; // 是否显示编辑弹窗
					this.editMixin_otherLoading = false; // 编辑其他信息的加载状态
					this.editMixin_isShowEditDialog = true;
				}
			});
		},
		/**
		 * 编辑弹窗,基础信息部分点击提交
		 * @param {object} data 表单基础数据
		 * @param {object} itemsDetail 表单内的下拉选中详情
		 */
		editMixin_baseEditSubmit(data, itemsDetail) {
			this.editMixin_baseLoading = true;
			coursewareApi.editResourceBase({
				data: {
                    teacherId: data.teacherId,
					professorId: itemsDetail.teacher.userId,
					professorName: itemsDetail.teacher.userName,
					professorDptName: itemsDetail.teacher.dptName || '无',
					resourceId: this.editMixin_editInfo.resourceId,
					name: data.name,
					sectionNumber: data.sectionNumber,
                    auditState: this.oldCheckState,
                    livePlaybackState: data.livePlaybackState,
					sourceFileName: data.sourceFileName,
				},
				success: () => {
					this.$message.success("修改成功");
					this.onSubmit();
				},
				complete: () => {
					this.editMixin_baseLoading = false;
				}
			});
		},
		/**
		 * 编辑弹窗,项目信息部分点击提交
		 * @param {object} data 表单基础数据
		 * @param {object} itemsDetail 表单内的下拉选中详情
		 * @param {function} callback 提交回调，用于告诉子组件是否提交成功
		 */
		editMixin_projectEditSubmit(data, itemsDetail, callback) {
			const { project, subject, course, year, period } = itemsDetail;
			const courseEnd = course.filter((i) => i.level === 2);
			this.editMixin_projectLoading = true;
            let NotSharedParams = {};
            // 待审核和审核不通过资源单独的传参
            if(this.pageType !== "search") {
                NotSharedParams.auditState = this.oldCheckState;
            }
			coursewareApi[this.pageType === "search" ? "editAuditPassResourceProject" : "editAuditNotResourceProject"]({
				data: {
					projectId: data.projectId,
					projectName: project.find((i) => i.value === data.projectId).label,
					examCycleId: period.pypId,
					examCycleName: period.pypseqName,
					subjectList: subject.map((i) => ({ subjectId: i.subjectId, subjectName: i.subjectName })),
					seriesCourseList: courseEnd.map((i) => ({
						seriesCourseId: i.value,
						seriesCourseName: i.label,
						teachRange: i.parent.value
					})),
					applicableYear: year.projectyearYear,
					applicableYearId: year.projectyearId,
					resourceId: this.editMixin_editInfo.resourceId,
					originalProjectId: data.originalProjectId || data.projectId,
					chapterList: data.rcChapterList,
					knowledgeList: data.rcCknowledgeList,
                    ...NotSharedParams
				},
				success: () => {
					callback(true);
					this.$message.success("修改成功");
					this.onSubmit();
				},
				complete: () => {
					this.editMixin_projectLoading = false;
				}
			});
		},
		/**
		 * 编辑弹窗,其他信息部分点击提交
		 */
		editMixin_otherEditSubmit(params) {
			this.editMixin_otherLoading = true;
			coursewareApi.updateResourceOtherMessage({
				data: {
					...params,
                    auditState: this.oldCheckState
				},
				success: () => {
					this.$message.success("修改成功");
					this.onSubmit();
				},
				complete: () => {
					this.editMixin_otherLoading = false;
				}
			});
		},
		/**
		 * 编辑弹窗,项目信息部分已保存的项目点击删除
		 * @param {string} projectId 要删除的项目id
		 * @param {function} callback 提交回调，用于告诉子组件是否提交成功
		 */
         editMixin_projectEditDelete(projectId, callback) {
			this.editMixin_projectLoading = true;
			coursewareApi.delResourceProject({
				data: {
					projectId,
					resourceId: this.editMixin_editInfo.resourceId
				},
				success: () => {
					callback(true);
					this.$message.success("删除成功");
				},
				complete: () => {
					this.editMixin_projectLoading = false;
				}
			});
		}
	}
};
